import apiPath from '@/utils/apiPath';
import request from '@/utils/request';
import { setAuthority } from '@/utils/authority';
import { getStore } from '@/utils/utils';

const UserModel = {
  namespace: 'user',
  state: {
    currentUser: {},
  },
  effects: {
    *fetchCurrent(_, { call, put }) {
      let token = getStore({
        name: 'USER_TOKEN'
      });
      
      if (!token) {
        yield put({
          type: 'login/logout'
        });
        return;
      }
      let userRes = yield request(apiPath.GET_USER_INFO, {
        method: 'GET',
        params: {
          phone: '13128641866'
        },
      });
      console.log(userRes.data);
      yield put({
        type: 'saveCurrentUser',
        payload: userRes.data
      });
      setAuthority(
        userRes.data.auths
      );
    },
  },
  reducers: {
    saveCurrentUser(state, action) {
      return { ...state, currentUser: action.payload || {} };
    },

    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
        },
      };
    },
  },
};
export default UserModel;
